package io.github.uicdb.et.item.weapon;

import com.google.common.collect.ImmutableMap;
import io.github.uicdb.et.ModElements;
import io.github.uicdb.et.ModItemDataComponents;
import io.github.uicdb.et.element.BasicElement;
import io.github.uicdb.et.item_data.ItemElementData;
import mod.azure.azurelib.neoforge.event.GeoRenderEvent;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.Position;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.entity.projectile.Snowball;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ProjectileItem;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

public class Gust extends Item implements IExtraDamageProvider {


    public Gust() {
        super(new Properties().durability(500)
                .component(ModItemDataComponents.ITEM_ELEMENT_DATA, new ItemElementData(ModElements.WOOD.getElemental().getElementHolder(), 8, 30)
                )
        );
    }


    @Override
    public void appendElementMap(ImmutableMap.@NotNull Builder<Holder<BasicElement>, Float> builder) {
        builder.put(ModElements.WOOD.getElemental().getElementHolder(), 0.04f);
    }

    private Map<Holder<BasicElement>, Float> elementMap;

    @Override
    public Map<Holder<BasicElement>, Float> getElementMap() {
        if (elementMap == null) {
            var builder = ImmutableMap.<Holder<BasicElement>, Float>builder();
            appendElementMap(builder);
            elementMap = builder.build();
        }
        return elementMap;
    }

    @Override
    public @NotNull InteractionResultHolder<ItemStack> use(Level level, Player player, @NotNull InteractionHand hand) {
        ItemStack itemstack = player.getItemInHand(hand);
        level.playSound(
                null,
                player.getX(),
                player.getY(),
                player.getZ(),
                SoundEvents.SNOWBALL_THROW,
                SoundSource.NEUTRAL,
                0.5F,
                0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)
        );
        if (!level.isClientSide) {
            Projectile projectile = new Snowball(level, player);
            projectile.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F);
            level.addFreshEntity(projectile);
        }

        player.awardStat(Stats.ITEM_USED.get(this));
        if (!player.getAbilities().instabuild)
            itemstack.hurtAndBreak(1, player, EquipmentSlot.MAINHAND);
        return InteractionResultHolder.sidedSuccess(itemstack, level.isClientSide());
    }

}
